package com.yingli.storage.provider;

import android.Manifest;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Contacts;
import android.util.Log;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.yingli.storage.R;
import com.yingli.storage.db.DBDemo;

import java.util.List;
import java.util.Map;

public class ContactActivity extends AppCompatActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_contact);

        DBDemo demo = new DBDemo(this);
        findViewById(R.id.btn_save).setOnClickListener(v->{
            demo.insertStudent("张磊1",15);
            demo.insertStudent("张磊2",18);
            demo.insertStudent("张磊3",17);
        });

        findViewById(R.id.btn_query).setOnClickListener(v->{
            List<Map<String,String>> stus = demo.getAllStu();
            Log.i("students",stus.toString());
        });
        EditText etAge = findViewById(R.id.et_age);
        findViewById(R.id.btn_query_age).setOnClickListener(v->{
            List<Map<String,String>> stus = demo.getStusOlderThan(Integer.parseInt(etAge.getText().toString()));
            Log.i("students",stus.toString());
        });

        findViewById(R.id.btn_delete).setOnClickListener(v->{
            demo.deleteStudent(etAge.getText().toString());
        });

        findViewById(R.id.btn_update).setOnClickListener(v->{
            demo.updateStudent("小马哥",16);
        });
        PermissionsUtils.getInstance().checkPermissions(this, new String[]{Manifest.permission.READ_CONTACTS}, new PermissionsUtils.IPermissionsResult() {
            @Override
            public void passPermissons() {
//                displayRecords();
            }

            @Override
            public void forbitPermissons() {

            }
        });

    }

//    private void displayRecords() {
//        String [] columns = {Contacts.People.NAME,Contacts.People.NUMBER};
//        Uri uri = Contacts.People.CONTENT_URI;
//        Cursor cursor = managedQuery(uri,columns,null,null,null);
//        if (cursor.moveToFirst()){
//            String name = null;
//            String phone = null;
//            do{
//                name = cursor.getString(cursor.getColumnIndex(Contacts.People.NAME));
//                phone = cursor.getString(cursor.getColumnIndex(Contacts.People.NUMBER));
//                Toast.makeText(this,name+":"+phone,Toast.LENGTH_SHORT).show();
//            }while (cursor.moveToNext());
//        }
//    }
}
